


/**
 *
 * @param widgetId
 * @param treeData
 * @param showlevels
 */
function initTreeViewById(widgetId,treeData,showlevels,loadAddNodeMethod){
    var treeShowlevels = 2;
    if(null!=showlevels || ""!=showlevels){
        treeShowlevels = showlevels;
    }
    var control  = $('#' + widgetId);
    control.treeview({
        data: treeData,
        silent: true,
        levels: treeShowlevels,   //展示层级控制
        multiSelect: false,  //多选
        onNodeSelected: function(event, node) { //选中节点
            if (typeof loadAddNodeMethod === 'function') {
                var addNodeList = loadAddNodeMethod(node.id);
                var addNodeArray =new Array;
                var allNodes =  getNodeIdArr(node);
                //先判断获取的数据是否已经加载，如果已经加载，则不add进去
               for(var anl =0 ;anl <addNodeList.length;anl++){
                   var isAdd = true;
                   var mAddNode = addNodeList[anl];
                   if(null!=allNodes && allNodes.length >0){
                       for(var nodeIndex = 0 ;nodeIndex < allNodes.length ; nodeIndex ++ ){
                           var existsNode =  allNodes[nodeIndex];
                           if (existsNode.id == mAddNode.id   ) {
                               isAdd = false;
                               break;
                           }
                       }
                   }
                   if(isAdd && null != mAddNode){
                       addNodeArray.push(mAddNode);
                   }
               }
               if(addNodeArray.length >0){
                    //  control.treeview("deleteNode", 12);   删除节点
                    control.treeview("addNode", [node.nodeId, { node: addNodeArray, silent: true }]);
                }
            }

            if(node.state.expanded){
                //处于展开状态则折叠
                //control.treeview('collapseNode', node.nodeId);
            } else {
                //展开
                control.treeview('expandNode', node.nodeId);
            }

        },
        onNodeUnselected: function(event, node) {
            /*
            if(node.state.expanded){
                //处于展开状态则折叠
                control.treeview('collapseNode', node.nodeId);
            } else {
                //展开
                control.treeview('expandNode', node.nodeId);
            }*/
        },
        onNodeExpanded:
            function(event, data) {
            }
    });
}

function canAddNodeToTree(node,mAddNode) {
    var isAdd = true;
    //递归获取所有的结点
   var allNodes =  getNodeIdArr(node);
   for(var j=0; j<allNodes.length; j++){
       var existsNode = allNodes[j];
       if (existsNode.id == mAddNode.id   ) {
           isAdd = false;
           break;
       }
   }
   return isAdd;
}

//递归获取所有的结点id
function getNodeIdArr( node ){
    var nodeList = [];
    var ts = [];
    if(node.nodes){
        for(x in node.nodes){
            ts.push(node.nodes[x].nodeId);
            nodeList.push(node.nodes[x]);
            if(node.nodes[x].nodes){
                var getNodeDieDai = getNodeIdArr(node.nodes[x]);
                for(j in getNodeDieDai){
                    ts.push(getNodeDieDai[j]);
                    nodeList.push(getNodeDieDai[j]);
                }
            }
        }
    }else{
        ts.push(node.nodeId);
        nodeList.push(node);
    }
    return nodeList;
}


//递归获取所有的结点id返回节点拼接内容
function getNodeShowText( node ){
    var showInfoText = "";
    var nodeList = [];
    var ts = [];
    if(node.nodes){
        for(x in node.nodes){
            ts.push(node.nodes[x].nodeId);
            nodeList.push(node.nodes[x]);
            if(node.nodes[x].nodes){
                var getNodeDieDai = getNodeIdArr(node.nodes[x]);
                for(j in getNodeDieDai){
                    ts.push(getNodeDieDai[j]);
                    nodeList.push(getNodeDieDai[j]);
                    showInfoText = showInfoText +"/" +getNodeDieDai[j].text;
                }
            }
        }
    }else{
        ts.push(node.nodeId);
        nodeList.push(node);
        showInfoText = showInfoText +"/" +node.text;
    }
    return showInfoText;
}

/**
 * 判断当前节点是否存在子节点
 * @param node
 * @param treeData
 */
function nodeIsExistsSubNodes(node){
    var haveSubNodes = false;
    var selId = node.id ;
    if(null != node.nodes && node.nodes.length >0 ){
        haveSubNodes = true;
    }
    return haveSubNodes;
}

/**
 * 实现点击也能展开的方法
 * @param target
 */
function itemOnclick(target){
    //找到当前节点id
    var nodeid = $(target).attr('data-nodeid');
    var tree = $('#tree');
    //获取当前节点对象
    var node = tree.treeview('getNode', nodeid);

    if(node.state.expanded){
        //处于展开状态则折叠
        tree.treeview('collapseNode', node.nodeId);
    } else {
        //展开
        tree.treeview('expandNode', node.nodeId);
    }
}


// 选中父节点时，选中所有子节点
function getChildNodeIdArr(node) {
    var ts = [];
    if (node.nodes) {
        for (x in node.nodes) {
            if(undefined ==node.nodes[x].nodeId || undefined ==node.nodes[x]){
                continue;
            }
            ts.push(node.nodes[x]);
            if (node.nodes[x].nodes) {
                var getNodeDieDai = getChildNodeIdArr(node.nodes[x]);
                for (j in getNodeDieDai) {
                    ts.push(getNodeDieDai[j]);
                }
            }
        }
    } else {
        ts.push(node.nodeId);
    }
    return ts;
}

// 取消父节点时 取消所有子节点
function setChildNodeUncheck(node) {
    if (node.nodes) {
        var ts = [];    //当前节点子集中未被选中的集合
        for (x in node.nodes) {
            if (!node.nodes[x].state.checked) {
                ts.push(node.nodes[x].nodeId);
            }
            if (node.nodes[x].nodes) {
                var getNodeDieDai = node.nodes[x];
                console.log(getNodeDieDai);
                for (j in getNodeDieDai) {
                    if (!getNodeDieDai.nodes[x].state.checked) {
                        ts.push(getNodeDieDai[j]);
                    }
                }
            }
        }
    }
    return ts;
}